package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.ServiceBean;
import com.sgb.kjwl.model.entity.enterprise.SecerTitleEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceChildAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceFirstAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceMainAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class NeedFragmentServicePopWindow extends BasePopWindow {

    private MaxHeightRecyclerView recyclerView;
    private MaxHeightRecyclerView childRecyclerView;
    private MaxHeightRecyclerView rlv_thread;
    private TextView okView;
    private TextView resetView;
    private TextView lineView;
    private TextView tv_s;
    private LinearLayout bottomLayout;
    private ServiceMainAdapter parentAreaAdapter;//一级适配器
    private ServiceFirstAdapter childAreaAdapter;//二级适配器
    private ServiceChildAdapter thredAreaAdapter;//三级适配器
    private List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> parentList;
//    private List<ParentAreaEntity<List<ParentAreaEntity>>>  childList;
//    private List<ParentAreaEntity> thridList;
//    private int parentPosition;
//    private int parentPosition2;
//    private int childPosition = 0;
//    private int childPosition2;
//    private int thridPosition;
//    private TwoAreaPopWindow.OnCloseBack onCloseBack;
    ServiceBean serviceBean = new ServiceBean();



    private ImageView image_z_frist,image_z_two,image_z_three;
    private View view_lin1,view_lin2;
    private LinearLayout lin_2,lin_3;

    public NeedFragmentServicePopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_needseveice;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        setAnimationStyle(com.swgk.core.R.style.popwindow_dropdown);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        childRecyclerView = holder.getPop().findViewById(R.id.rlv_child);
        rlv_thread=  holder.getPop().findViewById(R.id.rlv_thread);
        bottomLayout = holder.getPop().findViewById(R.id.ll_next);
        okView = holder.getPop().findViewById(R.id.tv_ok);
        resetView = holder.getPop().findViewById(R.id.tv_next);
        lineView = holder.getPop().findViewById(R.id.line_view);
        tv_s = holder.getPop().findViewById(R.id.tv_s);

        image_z_frist  = holder.getPop().findViewById(R.id.image_z_frist);
        image_z_two  = holder.getPop().findViewById(R.id.image_z_two);
        image_z_three  = holder.getPop().findViewById(R.id.image_z_two);

        view_lin1 = holder.getPop().findViewById(R.id.view_lin1);
        view_lin2 = holder.getPop().findViewById(R.id.view_lin2);

        lin_2 = holder.getPop().findViewById(R.id.lin_2);
        lin_3 = holder.getPop().findViewById(R.id.lin_3);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(false, serviceBean, null);
                }
                dissMiss();
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                serviceBean.setParentName("");//一级名称
                serviceBean.setParentNo("");//一级id
                serviceBean.setChildName("");//二级名称
                serviceBean.setChildNo("");
                serviceBean.setThridName("");
                serviceBean.setThridNo("");
                parentAreaAdapter.setChoiceindex(0);
                childAreaAdapter.setChoiceindex(0);
                thredAreaAdapter.setChoiceindex(0);
                childRecyclerView.setVisibility(View.GONE);
                rlv_thread.setVisibility(View.GONE);
                view_lin1.setVisibility(View.GONE);
                view_lin2.setVisibility(View.GONE);

                lin_2.setVisibility(View.GONE);
                lin_3.setVisibility(View.GONE);
                if (onResultClick != null) {
                    onResultClick.result(false, serviceBean, null);
                }
//                dissMiss();
            }
        });

        image_z_frist.setOnClickListener((View v)->{
            recyclerView.setVisibility(recyclerView.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
            image_z_frist.setImageResource(recyclerView.getVisibility()==View.VISIBLE?R.mipmap.btn_expand_up_gray:R.mipmap.btn_expand1);
        });

        image_z_two.setOnClickListener((View v)->{
            childRecyclerView.setVisibility(childRecyclerView.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
            image_z_two.setImageResource(childRecyclerView.getVisibility()==View.VISIBLE?R.mipmap.btn_expand_up_gray:R.mipmap.btn_expand1);
        });
        image_z_three.setOnClickListener((View v)->{
            rlv_thread.setVisibility(rlv_thread.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE);
            image_z_three.setImageResource(rlv_thread.getVisibility()==View.VISIBLE?R.mipmap.btn_expand_up_gray:R.mipmap.btn_expand1);
        });
        parentAreaAdapter = new ServiceMainAdapter(mContext, null);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(parentAreaAdapter);
        parentAreaAdapter.setItemSelect(true);
        parentAreaAdapter.setDefItem(0);
        parentAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>> listParentAreaEntity, int position) {
                tv_s.setText(listParentAreaEntity.getName());
                serviceBean.setParentName(listParentAreaEntity.getName());//一级名称
                serviceBean.setParentNo(listParentAreaEntity.getId());//一级id
                parentAreaAdapter.setChoiceindex(position);
                if (listParentAreaEntity.getName().equals("不限")) {

                    serviceBean.setChildName("");//二级名称
                    serviceBean.setChildNo("");
                    serviceBean.setThridName("");
                    serviceBean.setThridNo("");

                    childRecyclerView.setVisibility(View.GONE);
                    rlv_thread.setVisibility(View.GONE);
                    view_lin1.setVisibility(View.GONE);
                    view_lin2.setVisibility(View.GONE);
                    lin_2.setVisibility(View.GONE);
                    lin_3.setVisibility(View.GONE);
                } else {

                    showChildList(listParentAreaEntity);
                }
            }
        });

        childAreaAdapter = new ServiceFirstAdapter(mContext, null);
        childRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        childRecyclerView.setAdapter(childAreaAdapter);
        childAreaAdapter.setItemSelect(true);
        childAreaAdapter.setDefItem(0);
        childAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity<List<ParentAreaEntity>>>() {
            @Override
            public void onItemClick(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity, int position) {
                childAreaAdapter.setChoiceindex(position);
                tv_s.setText(listParentAreaEntity.getName());
                serviceBean.setChildName(listParentAreaEntity.getName());//二级名称
                serviceBean.setChildNo(listParentAreaEntity.getId());//二级id
                if (listParentAreaEntity.getName().equals("不限")) {
                    serviceBean.setThridName("");
                    serviceBean.setThridNo("");


                    rlv_thread.setVisibility(View.GONE);
                    view_lin2.setVisibility(View.GONE);
                    lin_3.setVisibility(View.GONE);
                }else {

                    showthridList(listParentAreaEntity);
                }
            }
        });


        thredAreaAdapter = new ServiceChildAdapter(mContext,null);
        rlv_thread.setLayoutManager(new GridLayoutManager(mContext, 4));
        rlv_thread.setAdapter(thredAreaAdapter);
        thredAreaAdapter.setItemSelect(true);
        thredAreaAdapter.setDefItem(0);
        thredAreaAdapter.setOnItemClick(new AdapterOnItemClick<ParentAreaEntity>() {
            @Override
            public void onItemClick(ParentAreaEntity parentAreaEntity, int position) {
                tv_s.setText(parentAreaEntity.getName());
                thredAreaAdapter.setChoiceindex(position);
                serviceBean.setThridName(parentAreaEntity.getName());
                serviceBean.setThridNo(parentAreaEntity.getId());
            }
        });
    }

    public void setData(List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> parentList1) {
        this.parentList = parentList1;
        if(parentList.size()>0&&!"不限".equals(parentList.get(0).getName())){
            ParentAreaEntity data = new ParentAreaEntity();
            data.setName("不限");
            data.setId("");
            parentList.add(0,data);
        }
        parentAreaAdapter.setDatas(parentList);
        if(parentList.size()>12){
            image_z_frist.setVisibility(View.VISIBLE);
        }else{
            image_z_frist.setVisibility(View.INVISIBLE);
        }

//        tv_s.setText(parentList.get(0).getName());
    }




    public void showChildList(ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>> listParentAreaEntity) {

        if(listParentAreaEntity.getChildren().size()>0){
         if(!"不限".equals(listParentAreaEntity.getChildren().get(0).getName())) {
             ParentAreaEntity data = new ParentAreaEntity();
             data.setName("不限");
             data.setId("");
             listParentAreaEntity.getChildren().add(0,data);
         }
        }else{
//            ParentAreaEntity data = new ParentAreaEntity();
//            data.setName("不限");
//            data.setId("");
//            listParentAreaEntity.getChildren().add(0,data);
            childRecyclerView.setVisibility(View.GONE);
            view_lin1.setVisibility(View.GONE);
            lin_2.setVisibility(View.GONE);
            return;
        }


        childAreaAdapter.setDefItem(0);
        serviceBean.setChildName(listParentAreaEntity.getChildren().get(0).getName());//二级名称
        serviceBean.setChildNo(listParentAreaEntity.getChildren().get(0).getId());//二级id

        childAreaAdapter.setDatas(listParentAreaEntity.getChildren());
        childAreaAdapter.setChoiceindex(0);


        childRecyclerView.setVisibility(View.VISIBLE);
        rlv_thread.setVisibility(View.GONE);
        view_lin1.setVisibility(View.VISIBLE);
        view_lin2.setVisibility(View.GONE);

        lin_2.setVisibility(View.VISIBLE);
        lin_3.setVisibility(View.GONE);

        if(listParentAreaEntity.getChildren().size()>12){
            image_z_two.setVisibility(View.VISIBLE);
        }else{
            image_z_two.setVisibility(View.INVISIBLE);
        }


    }




    public void showthridList(ParentAreaEntity<List<ParentAreaEntity>> listParentAreaEntity) {
        if(listParentAreaEntity.getChildren().size()>0){
            if(!"不限".equals(listParentAreaEntity.getChildren().get(0).getName())) {
                ParentAreaEntity data = new ParentAreaEntity();
                data.setName("不限");
                data.setId("");
                listParentAreaEntity.getChildren().add(0,data);
            }
        }else{
//            ParentAreaEntity data = new ParentAreaEntity();
//            data.setName("不限");
//            data.setId("");
//            listParentAreaEntity.getChildren().add(0,data);
            rlv_thread.setVisibility(View.GONE);
            view_lin2.setVisibility(View.GONE);
            lin_3.setVisibility(View.GONE);
            return;
        }

        thredAreaAdapter.setDefItem(0);
        serviceBean.setThridName(listParentAreaEntity.getChildren().get(0).getName());//二级名称
        serviceBean.setThridNo(listParentAreaEntity.getChildren().get(0).getId());//二级id

        thredAreaAdapter.setDatas(listParentAreaEntity.getChildren());
        thredAreaAdapter.setChoiceindex(0);


        rlv_thread.setVisibility(View.VISIBLE);
        view_lin2.setVisibility(View.VISIBLE);
        lin_3.setVisibility(View.VISIBLE);

        if(listParentAreaEntity.getChildren().size()>12){
            image_z_three.setVisibility(View.VISIBLE);
        }else{
            image_z_three.setVisibility(View.INVISIBLE);
        }


    }


    public void showMoren() {

        serviceBean.setParentName("");//一级名称
        serviceBean.setParentNo("");//一级id
        serviceBean.setChildName("");//二级名称
        serviceBean.setChildNo("");
        serviceBean.setThridName("");
        serviceBean.setThridNo("");
        parentAreaAdapter.setChoiceindex(0);
        childAreaAdapter.setChoiceindex(0);
        thredAreaAdapter.setChoiceindex(0);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null) {
            onDisMiss.disMiss();
        }
    }

    public void setFirstPosition(int position) {
        parentAreaAdapter.setChoiceindex(position);
        if (serviceBean != null) {
            serviceBean.setParentNo(parentAreaAdapter.getDatas().get(position).getId());
        }

    }
}
